package com.adopapa.nezhacloud.register.service;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.adopapa.nezhacloud.common.Tools;
import com.adopapa.nezhacloud.ddd.service.BaseDomainService;
import com.adopapa.nezhacloud.register.domain.StudentInfo;
import com.adopapa.nezhacloud.register.domain.StudentInfoRepository;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;


@Service
public class StudentInfoService extends BaseDomainService<StudentInfo> {

	@Autowired
	private StudentInfoRepository studentInfoRepository;

	public StudentInfo getStudentInfoById(String id) {
		Optional<StudentInfo> optional = studentInfoRepository.findById(id);
		return optional.isPresent() ? optional.get() : null;
	}
	
	public StudentInfo getStudentInfoByCellphone(String cellphone) {
		List<StudentInfo> results = studentInfoRepository.findByCellphone(cellphone);
		return Tools.empty(results) ? null : results.get(0);
	}

	public Page<StudentInfo> getStudentInfoPage(int pageNo, int pageSize, StudentInfo studentInfo) {
		PageRequest pageRequest = buildPageRequest(pageNo, pageSize, Order.desc("createTime"));
		Specification<StudentInfo> specification = buildSpecification(studentInfo);
		Page<StudentInfo> page = studentInfoRepository.findAll(specification, pageRequest);
		return page;
	}

	public StudentInfo saveStudentInfo(StudentInfo studentInfo) {
		return studentInfoRepository.saveAndFlush(studentInfo);
	}

	public List<StudentInfo> saveStudentInfos(List<StudentInfo> studentInfos) {
		return studentInfoRepository.saveAllAndFlush(studentInfos);
	}

	public void deleteStudentInfo(String id) {
		studentInfoRepository.deleteById(id);
	}

	public void deleteStudentInfo(StudentInfo studentInfo) {
		studentInfoRepository.delete(studentInfo);
	}

	public void deleteStudentInfos(String... ids) {
		studentInfoRepository.deleteAllByIdInBatch(Arrays.asList(ids));
	}

	public void deleteStudentInfos(List<StudentInfo> studentInfos) {
		studentInfoRepository.deleteAllInBatch(studentInfos);
	}

	@SuppressWarnings("serial")
	@Override
	public Specification<StudentInfo> buildSpecification(StudentInfo studentInfo) {
		return new Specification<StudentInfo>() {
			@Override
			public Predicate toPredicate(Root<StudentInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicates = new LinkedList<>();
				if (!Tools.empty(studentInfo.getName())) {
					predicates.add(cb.like(root.get("name"), //
						"%".concat(studentInfo.getName()).concat("%")));
				}
				if (!Tools.empty(studentInfo.getCellphone())) {
					predicates.add(cb.equal(root.get("cellphone"), //
						studentInfo.getCellphone()));
				}
				if (!Tools.empty(studentInfo.getCareer())) {
					predicates.add(cb.equal(root.get("career"), //
						studentInfo.getCareer()));
				}
				if (!Tools.empty(studentInfo.getProvince())) {
					predicates.add(cb.equal(root.get("province"), //
						studentInfo.getProvince()));
				}
				if (!Tools.empty(studentInfo.getCity())) {
					predicates.add(cb.equal(root.get("city"), //
						studentInfo.getCity()));
				}
				if (!Tools.empty(studentInfo.getTown())) {
					predicates.add(cb.equal(root.get("town"), //
						studentInfo.getTown()));
				}
				if (!Tools.empty(studentInfo.getDoulin())) {
					predicates.add(cb.equal(root.get("doulin"), //
						studentInfo.getDoulin()));
				}
				if (!Tools.empty(studentInfo.getFans())) {
					predicates.add(cb.equal(root.get("fans"), //
						studentInfo.getFans()));
				}
				if (!Tools.empty(studentInfo.getCreateTime())) {
					predicates.add(cb.greaterThanOrEqualTo(root.get("createTime"), //
						studentInfo.getCreateTime()));
				}
				if (!Tools.empty(studentInfo.getCreateTime2())) {
					predicates.add(cb.lessThan(root.get("createTime2"), //
						studentInfo.getCreateTime2()));
				}
				return cb.and(predicates.toArray(new Predicate[0]));
			}

		};
	}
	
	// predicates.add(cb.equal(cb.substring(root.get("datagram"), 1, 23), dataPrefix));

}
